package dao

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type AccessControl struct {
	Id                int    `json:"id" gorm:"primary_key" description:"自增主键"`
	ServiceId         int    `json:"service_id" gorm:"column:service_id" description:"服务Id"`
	OpenAuth          int    `json:"open_auth" gorm:"column:open_auth" description:"是否开启权限 1开启"`
	BlackList         string `json:"black_list" gorm:"column:black_list" description:"黑名单IP"`
	WhiteList         string `json:"white_list" gorm:"column:white_list" description:"白名单IP"`
	WhiteHostName     string `json:"white_host_name" gorm:"column:white_host_name" description:"白名单主机"`
	ClientIPFlowLimit int    `json:"clientip_flow_limit" gorm:"column:clientip_flow_limit" description:"客户端IP限流"`
	ServiceFlowLimit  int    `json:"service_flow_limit" gorm:"column:service_flow_limit" description:"服务端限流"`
}

func (a *AccessControl) TableName() string {
	return "gateway_service_access_control"
}

func (a *AccessControl) Find(c *gin.Context, tx *gorm.DB, search *AccessControl) (*AccessControl, error) {
	accessControl := &AccessControl{}
	// 根据结构体查询
	err := tx.WithContext(c).Where(search).First(accessControl).Error
	if err != nil {
		return nil, err
	}
	return accessControl, nil
}

func (a *AccessControl) Save(c *gin.Context, tx *gorm.DB) error {
	err := tx.WithContext(c).Save(a).Error
	return err
}
